package com.nettest.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalUtil {	
	public static BigDecimal multiply(Number... numbers) {
		if (numbers == null) {
			return BigDecimal.ZERO;
		}
		
		if (numbers.length == 1) {
			return new BigDecimal(numbers[0].toString());
		}
		
		BigDecimal result = BigDecimal.ONE;
		for (Number n : numbers) {
			result = multiply(result, n);
		}
		return result;		
	}
	public static BigDecimal subtract(Number... numbers) {
		if (numbers == null) {
			return BigDecimal.ZERO;
		}
		
		if (numbers.length == 1) {
			return new BigDecimal(numbers[0].toString());
		}
		
		BigDecimal result = new BigDecimal(numbers[0].toString());
		for (int i = 1; i < numbers.length; i++) {
			result = subtract(result, numbers[i]);
		}
		return result;		
	}
	public static BigDecimal add(Number... numbers) {
		if (numbers == null) {
			return BigDecimal.ZERO;
		}
		
		if (numbers.length == 1) {
			return new BigDecimal(numbers[0].toString());
		}
		
		BigDecimal result = BigDecimal.ZERO;
		for (Number n : numbers) {
			result = add(result, n);
		}
		return result;		
	}
	
	public static BigDecimal divide(Number... numbers) {
		if (numbers == null) {
			return BigDecimal.ZERO;
		}
		
		if (numbers.length == 1) {
			return new BigDecimal(numbers[0].toString());
		}
		
		BigDecimal result = new BigDecimal(numbers[0].toString());
		for (int i = 1; i < numbers.length; i++) {
			result = divide(result, numbers[i]);
		}
		return result;		
	}	
    public static BigDecimal multiply(Number n1, Number n2) {
    	return multiply(n1, n2, 2, RoundingMode.HALF_UP, 2, RoundingMode.HALF_UP);
    }
        
    public static BigDecimal subtract(Number n1, Number n2) {
    	return subtract(n1, n2, 2, RoundingMode.HALF_UP, 2, RoundingMode.HALF_UP);
    }
    
    public static BigDecimal add(Number n1, Number n2) {
    	return add(n1, n2, 2, RoundingMode.HALF_UP, 2, RoundingMode.HALF_UP);
    }    
    
    public static BigDecimal divide(Number n1, Number n2) {
    	return divide(n1, n2, 2, RoundingMode.HALF_UP, 2, RoundingMode.HALF_UP);
    }
        
    public static BigDecimal multiply(Number n1, Number n2, int firstScale, RoundingMode firstMode, int secondScale, RoundingMode secondMode) {
    	BigDecimal bigN1 = toScalePrecision(n1, firstScale, firstMode);
    	BigDecimal bigN2 = toScalePrecision(n2, secondScale, secondMode);  
    	
    	return bigN1.multiply(bigN2);
    }
    
    public static BigDecimal subtract(Number n1, Number n2, int firstScale, RoundingMode firstMode, int secondScale, RoundingMode secondMode) {    	
    	BigDecimal bigN1 = toScalePrecision(n1, firstScale, firstMode);
    	BigDecimal bigN2 = toScalePrecision(n2, secondScale, secondMode);    	
    	
    	return bigN1.subtract(bigN2);
    }
    
    public static BigDecimal add(Number n1, Number n2, int firstScale, RoundingMode firstMode, int secondScale, RoundingMode secondMode) {    	
    	BigDecimal bigN1 = toScalePrecision(n1, firstScale, firstMode);
    	BigDecimal bigN2 = toScalePrecision(n2, secondScale, secondMode);    	
    	
    	return bigN1.add(bigN2);
    }
    
    public static BigDecimal divide(Number n1, Number n2, int firstScale, RoundingMode firstMode, int secondScale, RoundingMode secondMode) {    	
    	BigDecimal bigN1 = toScalePrecision(n1, firstScale, firstMode);
    	BigDecimal bigN2 = toScalePrecision(n2, secondScale, secondMode);    	
    	
    	return bigN1.divide(bigN2);
    }

    public static BigDecimal toScalePrecision(Number n, int scale, RoundingMode mode) {
    	if (CommonUtil.isExtNull(n)) {
    		return BigDecimal.ZERO;
    	}
    	
    	BigDecimal bigN = new BigDecimal(n.toString());
    	if (scale != -1 && n.toString().contains(".")) {
    		bigN = bigN.setScale(scale, mode);
    	}
    	
    	return bigN;
    }
}
